package com.ruoyi.system.https.bo;


import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.ruoyi.system.domain.SapData;
import lombok.Data;
import lombok.ToString;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Data
@Log4j2
@JacksonXmlRootElement(localName = "Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
@ToString
public class ZWS_PP_GET_PWOI_RESPONSE {

    @JacksonXmlProperty(localName = "Body", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
    private ZWS_PP_GET_PWOI_Body body;

    public ZWS_PP_GET_PWOI_Body getBody() {
        return body;
    }

    public void setBody(ZWS_PP_GET_PWOI_Body body) {
        this.body = body;
    }

    @ToString
    public static class ZWS_PP_GET_PWOI_Body{
        @JacksonXmlProperty(localName = "ZmesGetPwoiResponse", namespace = "urn:sap-com:document:sap:soap:functions:mc-style")
        private ZmesGetPwoiResponse zmesGetPwoiResponse;

        // Getters and Setters
        public ZmesGetPwoiResponse getZmesGetPwoiResponse() {
            return zmesGetPwoiResponse;
        }
        public  void setZmesGetPwoiResponse( ZmesGetPwoiResponse zmesGetPwoiResponse){
            this.zmesGetPwoiResponse=  zmesGetPwoiResponse;
        }


    }
    @ToString
    public static class ZmesGetPwoiResponse {

        @JacksonXmlProperty(localName = "Pwoi")
        private Pwoi pwoi;

        // Getters and Setters
        public Pwoi getPwoi() {
            return pwoi;
        }

        public void setPwoi(Pwoi pwoi) {
            this.pwoi = pwoi;
        }
    }
    @ToString
    public static class Pwoi {

        @JacksonXmlElementWrapper(useWrapping = false)
        @JacksonXmlProperty(localName = "item")
        private List<Item> items;

        // Getters and Setters
        public List<Item> getItems() {
            return items;
        }

        public void setItems(List<Item> items) {
            this.items = items;
        }
    }
    @Data
    @ToString
    public static class Item{
        @JacksonXmlProperty(localName = "Aufnr")
        private String aufnr;

        @JacksonXmlProperty(localName = "Posnr")
        private String posnr;

        @JacksonXmlProperty(localName = "Baugr")
        private String baugr;

        @JacksonXmlProperty(localName = "Maktx2")
        private String maktx2;

        @JacksonXmlProperty(localName = "Matnr")
        private String matnr;

        @JacksonXmlProperty(localName = "Maktx")
        private String maktx;

        @JacksonXmlProperty(localName = "Werks")
        private String werks;

        @JacksonXmlProperty(localName = "Arbpl")
        private String arbpl;

        @JacksonXmlProperty(localName = "Lgort")
        private String lgort;

        @JacksonXmlProperty(localName = "Prvbe")
        private String prvbe;

        @JacksonXmlProperty(localName = "Charg")
        private String charg;

        @JacksonXmlProperty(localName = "Bdmng")
        private double bdmng;

        @JacksonXmlProperty(localName = "Meins")
        private String meins;

        @JacksonXmlProperty(localName = "Potx1")
        private String potx1;

        @JacksonXmlProperty(localName = "Vornr")
        private String vornr;

        @JacksonXmlProperty(localName = "Ltxa1")
        private String ltxa1;

        @JacksonXmlProperty(localName = "Rsnum")
        private String rsnum;

        @JacksonXmlProperty(localName = "Rspos")
        private String rspos;

        @JacksonXmlProperty(localName = "Plnnr")
        private String plnnr;

    }
    //对象映射
    public static List<SapData> bodyToSapData(ZWS_PP_GET_PWOI_RESPONSE zws_pp_get_pwoi_respose) {
        List<SapData> sapDatas = new ArrayList<>();
        List<Item> aItems = zws_pp_get_pwoi_respose.getBody().getZmesGetPwoiResponse().getPwoi().getItems();
        for (Item aItem : aItems) {
            if(StringUtils.isNotBlank(aItem.getAufnr() )){
                SapData sapData = new SapData();
                sapData.setOrderNo(aItem.getAufnr());
                sapData.setIngreNo(aItem.getMatnr());
                sapData.setIngreDesc(aItem.getMaktx().replaceAll("'", ""));//去除单引号
                sapData.setPlant(aItem.getWerks());
                sapData.setSloc(aItem.getLgort());
                sapData.setSupplyPlant(aItem.getPrvbe());
                sapData.setBatch(aItem.getCharg());
                sapData.setCreateTime(LocalDateTime.now());

                try {
                    sapData.setQty(new BigDecimal(aItem.getBdmng()));
                }catch (Exception e){
                    log.error("{}:{}",aItem.getBdmng(),e.getMessage());
                }

                sapData.setUnit(aItem.getMeins());
                sapData.setOperation(aItem.getVornr());
                sapData.setRemark(aItem.getPotx1());
                sapData.setOperation(aItem.getVornr());
                sapData.setStep(aItem.getPosnr());
                try {
                    sapData.setReserve1(Integer.parseInt(aItem.getRsnum()));
                }catch (Exception e){
                    log.error("{}:{}",aItem.getRsnum(),e.getMessage());
                }
                try {
                    sapData.setReserve2(Integer.parseInt(aItem.getRspos()));
                }catch (Exception e){
                    log.error("{}:{}",aItem.getRspos(),e.getMessage());
                }

                sapData.setLatxa1(aItem.getLtxa1());
                sapData.setPlnnr(aItem.getPlnnr());
                sapData.setIsEnable(1);
                sapDatas.add(sapData);
            }
        }
        return  sapDatas;
    }

}
